package com.dreammore.framework.common.utils;

import java.io.IOException;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ResourceContentParser {
	//".gif", ".png", ".jpg", ".jpeg", ".bmp"
	private static Set<String> extNameSet = new HashSet<String>();
	static{
		extNameSet.add(".gif");
		extNameSet.add(".png");
		extNameSet.add(".jpg");
		extNameSet.add(".jpeg");
		extNameSet.add(".bmp");
	}
	
	private final static String imagePattern = "<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>";
	
	public static List<String> parseImages(String content){
		List<String> imageUrls = new LinkedList<String>(); 
		Pattern pattern = Pattern.compile(imagePattern); 
		Matcher matcher = pattern.matcher(content);
		
		boolean result = matcher.find();
		while(result){
			imageUrls.add(matcher.group(1));
			result = matcher.find();
		}
		
		return imageUrls;
	}

	
	 public static void main(String[] args) throws IOException {
		 String content = "<p>									</p>";
		 content += "<p style=\"text-align: center;\">";
		 content += "<img src=\"http://localhost:8080/capt/ueditor/jsp/upload1/20140322/1395481894280.jpg\" height=\"238\" width=\"338\"/></p>";
		 content += "<p style=\"text-align: center;\">";
		 content += "<span style=\"font-size: 13px; font-family: &#39;Times New Roman&#39;,&#39;serif&#39;;\">";
		 content += "<img src=\"http://localhost:8080/capt/ueditor/jsp/upload1/20140322/74421395482186219.jpg\" title=\"Hydrangeas.jpg\"/></span></p>";
		 content += "<img src=\"http://localhost:8080/capt/ueditor/jsp/upload1/20140322/74421395482186219.jpg\" title=\"Hydrangeas.jpg\"/></span></p>";
		 content += "<img src=\"http://localhost:8080/capt/ueditor/jsp/upload1/20140322/74421395482186219.jpg\" title=\"Hydrangeas.jpg\"/></span></p>";
		 content += "<img src=\"http://localhost:8080/capt/ueditor/jsp/upload1/20140322/74421395482186219.jpg\" title=\"Hydrangeas.jpg\"/></span></p>";
		 
		 System.out.println(parseImages(content));
	 }
	 
	 
	 public static boolean getImg(String content) throws IOException { 
		 
		 boolean f=false; 
		 
		 String patternStr="<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>";
		 Pattern pattern = Pattern.compile(patternStr); 
		 
		 Matcher matcher = pattern.matcher(content);
		 boolean result = matcher.find();
		 while(result){
			 System.out.println(matcher.group(1));
			 result = matcher.find();
		 }
		 
//		 if (matcher.matches()) { 
//			 System.out.println(matcher.group());
//			 System.out.println(matcher.groupCount());
//			 System.out.println("searchImgReg1:"+matcher.group(1)); 
//			 
//			 
//		 }
		return f;  
	 }
}
